package cn.qvtu.web.domain;

import cn.qvtu.web.mapper.StatisticMapper;
import cn.qvtu.web.utility.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduleTask {

    @Autowired
    private MailUtils mailUtils;

    @Value("${spring.mail.username}")
    private String mailto;

    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * cron 一共可以有7个参数 以空格分开 其中年不是必须参数
     * [秒] [分] [小时] [日] [月] [周] [年]，
     */
    @Scheduled(cron = "0 0 12 1 * ?")//定时邮件发送任务，每月1日中午12点整发送邮件
    //@Scheduled(cron = "0 */3 * * * ? ")//定时邮件发送任务，每三分钟执行一次
    public void sendEmail(){
        long totalvisit = statisticMapper.getTotalVisit();
        long totalComment = statisticMapper.getTotalComment();
        StringBuffer content = new StringBuffer();
        content.append("博客系统总访问量为："+totalvisit+"人次").append("\n");
        content.append("博客系统总评论量为："+totalComment+"人次").append("\n");
        mailUtils.sendSimpleEmail(mailto,"个人博客系统流量统计情况",content.toString());
    }
}
